//
//  ViewController.m
//  Demos
//
//  Created by iMac on 2018/3/2.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "ViewController.h"



@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;

@end


CGFloat margin = 40;
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadDatas];
    self.view.backgroundColor = [UIColor grayColor];
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] init];
        tableView.frame = CGRectMake(margin, margin * 2, SCREEN_WIDTH - margin * 2, SCREEN_HEIGHT - margin * 4);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView;
    });
    [self.view addSubview:self.tableView];
    
}



#pragma mark - tableView delegate-datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"homeCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"homeCellID"];
    }
    NSDictionary *dict = self.datas[indexPath.row];
    cell.textLabel.text = dict[@"title"];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.datas[indexPath.row];
    NSString *controllerString = dict[@"controller"];
    Class cls = NSClassFromString(controllerString);
    UIViewController *controller = [[cls alloc] init];
    if ([controller isKindOfClass:[UIViewController class]]) {
        [self presentViewController:controller animated:YES completion:nil];
    }
}

#pragma mark load-data
- (void)loadDatas {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"controllers" ofType:@"plist"];
    self.datas = [NSArray arrayWithContentsOfFile:filePath];
}

@end
